﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace xEditor
{
    public partial class ParticleControl : UserControl
    {
        private GraphicsDevice _graphicsDevice;
        private PresentationParameters parameters;

        public void InitializeGD()
        {
            parameters = new PresentationParameters();

            parameters.BackBufferWidth = Math.Max(ClientSize.Width, 1);
            parameters.BackBufferHeight = Math.Max(ClientSize.Height, 1);
            parameters.BackBufferFormat = SurfaceFormat.Color;

            parameters.EnableAutoDepthStencil = true;
            parameters.AutoDepthStencilFormat = DepthFormat.Depth24;

            _graphicsDevice = new GraphicsDevice(GraphicsAdapter.DefaultAdapter,
                                    DeviceType.Hardware,
                                    Handle,
                                    parameters);

        }

        public void TestGD()
        {
            // Initialize Transform
            Matrix viewMatrix = Matrix.CreateLookAt(
                new Vector3(0.0f, 0.0f, 1.0f),
                Vector3.Zero,
                Vector3.Up
            );

            Matrix projectionMatrix = Matrix.CreateOrthographicOffCenter(
                0,
                (float)_graphicsDevice.Viewport.Width,
                (float)_graphicsDevice.Viewport.Height,
                0,
                1.0f, 1000.0f);
            
            VertexDeclaration vd = new VertexDeclaration(
                _graphicsDevice,
                VertexPositionColor.VertexElements)
            ;

            BasicEffect be = new BasicEffect(_graphicsDevice, null);
            be.VertexColorEnabled = true;
            
            Matrix worldMatrix = Matrix.CreateTranslation(_graphicsDevice.Viewport.Width / 2f - 150,
                _graphicsDevice.Viewport.Height / 2f - 50, 0);

            be.World = worldMatrix;
            be.View = viewMatrix;
            be.Projection = projectionMatrix;

            // Initialize Points
            vd = new VertexDeclaration(
                _graphicsDevice,
                VertexPositionColor.VertexElements
            );

            int points = 9;
            VertexPositionColor[] pointList = new VertexPositionColor[points];

            for (int x = 0; x < points / 2; x++)
            {
                for (int y = 0; y < 2; y++)
                {
                    pointList[(x * 2) + y] = new VertexPositionColor(
                        new Vector3(x * 100, y * 100, 0), Microsoft.Xna.Framework.Graphics.Color.White);
                }
            }

            // Initialize the vertex buffer, allocating memory for each vertex.
            VertexBuffer vb = new VertexBuffer(_graphicsDevice,
                VertexPositionColor.SizeInBytes * (pointList.Length),
                BufferUsage.None);

            // Set the vertex buffer data to the array of vertices.
            vb.SetData<VertexPositionColor>(pointList);

            // Draw
            _graphicsDevice.Clear(Microsoft.Xna.Framework.Graphics.Color.SteelBlue);
            _graphicsDevice.VertexDeclaration = vd;

            // The effect is a compiled effect created and compiled elsewhere
            // in the application.
            be.Begin();

            foreach (EffectPass pass in be.CurrentTechnique.Passes)
            {
                pass.Begin();

                _graphicsDevice.RenderState.PointSize = 10;

                _graphicsDevice.DrawUserPrimitives<VertexPositionColor>(
                    PrimitiveType.PointList,
                    pointList,
                    0,  // index of the first vertex to draw
                    8   // number of primitives
                );

                _graphicsDevice.RenderState.FillMode = FillMode.Solid;

                pass.End();
            }
            be.End();

            _graphicsDevice.Present();
        }

        public ParticleControl()
        {
            InitializeComponent();
            InitializeGD();
            TestGD();
        }


        /// <summary>
        /// Disposes the control.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (_graphicsDevice != null && disposing)
            {
                if (disposing)
                {
                    _graphicsDevice.Dispose();
                }

                _graphicsDevice = null;
            }

            base.Dispose(disposing);
        }
    }
}
